<?php

// event delete page
require_once 'view/common/layout.formview.php';
require_once 'view/common/layout.view.php';

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once APP_ROOT . 'model/common/event.php';
require_once 'view/pub/calendar/event.delete.php';
require_once APP_ROOT . "model/common/site.php";

class CalendarDeletePage extends PageController {

	var $form;


    function CalendarDeletePage() {
        parent::PageController();


        $this->form = new FormController();

        $this->form->addChild('delete', new ButtonController(new Delegate($this, 'delete')));
        $this->form->addChild('cancel', new ButtonController(new Delegate($this, 'cancel')));
        $this->form->setDefaultChild('cancel');

        $this->form->registerOnLoadListener(new Delegate($this, 'load'));

        $this->addChild('DeleteForm', $this->form);
        $this->setDefaultChild('DeleteForm');

        $this->registerOnActivateListener(new Delegate($this, 'guard'));


        $mytemplate = 'pub/calendar/event.delete.html';

        $this->setDefaultView(new Handle('EventDeleteView', array($mytemplate)));
        $this->addView('success', new Handle('RedirectView', array('/')));
        $this->addView('cancel', new Handle('RedirectView', array('/')));
        $this->addView('notfound', new Handle('RedirectView', array('/')));
    }

    function guard(&$source, &$request, &$responseModel) {
        if (!$request->hasParameter('event_id')) {
            print "Event profile was not found ";
            return 'notfound';
        }
    }

    function load(&$source, &$request, &$responseModel) {
    	$event_id = $request->getParameter('event_id');
        $record =& Event::getRecord($event_id);
        if (is_null($record)) {
            return 'notfound';
        }

        $responseModel->import($record->export());
    }

    function cancel(&$source, &$request, &$responseModel) {
        return 'cancel';
    }

    function delete(&$source, &$request, &$responseModel) {
    	print "Trying to delete...";
        if ($responseModel->isValid()) {
        	$name = $responseModel->get('title');
        	$event_id = $request->getParameter('event_id');
        	if ($event_id) {
            	Event::delete($event_id);
            	$responseModel->set('msg','The event '.$name.' has been deleted!');
            	return 'success';
            } else return 'notfound';
        }
    }

}


?>